﻿Imports System
Imports System.IO
Imports System.Globalization

Partial Public Class Stock_Management
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "STOCK MANAGEMENT "
            LoadData("", "")
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnNewStock.Visible = False
            End If
            If (Request.QueryString("P3") & "" <> "Y") Then
                gridStock.Columns(4).Visible = False
            End If
        End If
    End Sub

    Private Sub LoadData(ByVal Grade As String, ByVal Brand As String)
        ViewState("Stock") = clsGet.Get_StockMaster(Grade, Brand, "", "")
        gridStock.PageIndex = 0
        gridStock.DataSource = ViewState("Stock")
        gridStock.DataBind()
        gridStockAdd.DataSource = ViewState("Stock")
        gridStockAdd.DataBind()
    End Sub
    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridStock.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridStock.PageIndex = ddlPage.SelectedIndex
        gridStock.DataSource = ViewState("Stock")
        gridStock.DataBind()
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridStock.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridStock.PageIndex = 0
            Case "prev"
                If (gridStock.PageIndex <> 0) Then
                    gridStock.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridStock.PageIndex <> gridStock.PageCount) Then
                    gridStock.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridStock.PageIndex = gridStock.PageCount
        End Select
        gridStock.DataSource = ViewState("Stock")
        gridStock.DataBind()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadData("", "")
    End Sub

    Private Sub gridStock_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridStock.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridStock.PageIndex <> 0) Then
            Index = Index Mod (gridStock.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            Session("Stock") = gridStock.DataKeys(Index).Values("Stock_Code")
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "cmddelete" Then
            Dim Line_Code As String = gridStock.DataKeys(Index).Values("Stock_Code")
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            tran = clsDB.BeginTran(conn)
            If (clsFc.DeleteStock(Line_Code, conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                LoadData("", "")
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ลบข้อมูลเรียบร้อย!!');", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการลบข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub gridStock_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridStock.RowDataBound
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridStock.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridStock.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridStock.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridStock.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridStock_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridStock.RowEditing

    End Sub
    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadData(txtGrade.Text, txtBrand.Text)
    End Sub

    Private Sub btnNewStock_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNewStock.Click
        Session("Stock") = Nothing
        ModalPopupExtender1.Show()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub TabStock_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabStock.ActiveTabChanged
        LoadData("", "")
    End Sub

    Private Sub btnSearch2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch2.Click
        LoadData(txtGrade2.Text, txtBrand2.Text)
    End Sub

    Private Sub gridStockAdd_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridStockAdd.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (gridStockAdd.PageIndex <> 0) Then
            Index = Index Mod (gridStockAdd.PageIndex * 10)
        End If
        If e.CommandName = "cmdedit" Then
            panelAddRemove.Visible = True
            ddlAction.ClearSelection()
            txtAmount.Text = String.Empty
            txtRemark.Text = String.Empty
            ViewState("AddREmove") = gridStockAdd.DataKeys(Index).Values("Stock_Code")
            ViewState("Stock") = clsGet.Get_StockMaster("", "", gridStockAdd.DataKeys(Index).Values("Stock_Code"), "")
            gridStockAdd.DataSource = ViewState("Stock")
            gridStockAdd.DataBind()
        ElseIf (e.CommandName = "cmdActive") Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()

            '  tran = clsDB.BeginTran(conn)
            If (clsFc.Update_StockActive(gridStockAdd.DataKeys(Index).Values("Stock_Code"), gridStockAdd.DataKeys(Index).Values("Type_Additional"), gridStockAdd.DataKeys(Index).Values("Color"), conn, tran) > 0) Then
                LoadData(txtGrade2.Text, txtBrand2.Text)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ทำการ Active เสร็จสมบูรณ์!!');", True)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการ Active ข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        End If
    End Sub

    Private Sub gridStockAdd_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridStockAdd.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Dim btnActive As Button = CType(e.Row.FindControl("btnActive"), Button)
            If (gridStockAdd.DataKeys(e.Row.RowIndex).Values("Active") & "" = "Y") Then
                e.Row.Cells(3).Text = "Active"
                btnActive.Visible = False
            Else
                e.Row.Cells(3).Text = "In Active"
                btnActive.Visible = True
            End If
        ElseIf (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridStockAdd.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridStockAdd.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridStockAdd.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridStockAdd.PageCount.ToString()
            End If
        End If
    End Sub

    Private Sub gridStockAdd_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles gridStockAdd.RowEditing

    End Sub
    Protected Sub ddlPage2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridStockAdd.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridStockAdd.PageIndex = ddlPage.SelectedIndex
        gridStockAdd.DataSource = ViewState("Stock")
        gridStockAdd.DataBind()
    End Sub

    Public Sub Paginate2(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridStockAdd.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridStockAdd.PageIndex = 0
            Case "prev"
                If (gridStockAdd.PageIndex <> 0) Then
                    gridStockAdd.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridStockAdd.PageIndex <> gridStockAdd.PageCount) Then
                    gridStockAdd.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridStockAdd.PageIndex = gridStockAdd.PageCount
        End Select
        gridStockAdd.DataSource = ViewState("Stock")
        gridStockAdd.DataBind()
    End Sub
    Private Sub btnBack_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnBack.Click
        LoadData(txtGrade2.Text, txtBrand2.Text)
        panelAddRemove.Visible = False
        ViewState("AddREmove") = Nothing
    End Sub

    Private Sub btnConfirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConfirm.Click
        If (txtAmount.Text = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ Amount !!');", True)
            Exit Sub
        End If
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Dim ds As DataSet = Session("UserData")
        Dim User_Create As String = ds.Tables(0).Rows(0).Item("User_Name").ToString() & " " & DateTime.Now.ToString("dd MMM yyyy HH:mm:ss", New CultureInfo("en-US"))
        Application.Lock()
        conn.Open()

        tran = clsDB.BeginTran(conn)
        If (clsFc.Update_StockValue(ddlAction.SelectedValue, ViewState("AddREmove"), txtAmount.Text, conn, tran) > 0) Then
            If (clsFc.Insert_StockAdd_Remove(ViewState("AddREmove"), ddlAction.SelectedValue, txtAmount.Text, User_Create, "", txtRemark.Text, DateTime.Now.ToString("yyyyMMdd", New CultureInfo("en-US")), conn, tran) < 1) Then
                condition = False
            End If
        Else
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            LoadData(txtGrade2.Text, txtBrand2.Text)
            panelAddRemove.Visible = False
            ViewState("AddREmove") = Nothing
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub
End Class